<?php


namespace app\controller;


use think\Exception;
use think\facade\Log;
use think\facade\View;

class User
{
    public function index()
    {
        try {
            $userList = (new \app\model\User)->where(['limit' => 0, 'level' => 'in (0, 1)'])->order('create_time desc')->select();
            View::assign('list', $userList);
        } catch (Exception $e) {
            Log::error('查询失败, ' . $e->getMessage());
            View::assign('list', []);
        }
        return View::fetch();
    }

    public function deleteUser(){

        if (!request()->isPost()){
            return json(['code' => 500, 'message' => '不支持的请求方式']);
        }
        $param = request()->post();
        if (!isset($param['uid']) && !array_key_exists('uid', $param)){
            return json(['code' => 400, 'message' => '参数异常']);
        }
        $deleteUid = $param['uid'];
        $updateResult = \app\model\User::update(['limit' => 1], ['uid' => $deleteUid]);
        if ($updateResult){
            return json(['code' => 200, 'message' => '删除成功']);
        }else{
            return json(['code' => 500, 'message' => '删除失败, 请联系管理员']);
        }
    }
}